var web = require("../../utils/buyingWeb.js");
var interval;
Page({
  data: {
    // 是否模拟开盘
    isMoni: my.getStorageSync({ key: 'isMoni' }).data,

    // 当前批次剩余时间  分钟  秒
    minute: "",
    second: "",
    lastWarning: false,

    isCollected: false,
    collectionIcon: "../../image/collection.png",
    collectionText: "添加收藏",

    houseId: "",
    taskName: "",
    houseNo: "",
    selStartTime: "",

    buyerName: "",
    certifyCardType: "身份证",
    cardId: "",
    totalPrice: "",
    modalOpened: false,
    // 是否能第二次点击
    secondClick: true,

    canBuy: false,
    buttonBackground: "rgba(157,157,157,1)",

    taskType: "房"
  },
  onLoad(query) {
    if (query.isCollected == "true") {
      this.setData({
        isCollected: true,
        collectionIcon: "../../image/collected.png",
        collectionText: "取消收藏"
      });
    }
    var buttonBackground = "rgba(157,157,157,1)";
    var canBuy = false;
    if (query.canBuy == "true") {
      canBuy = true;
      buttonBackground = "rgba(38,128,235,1)";
      // 启动计时器
      this.countTime(parseInt(query.minute), parseInt(query.second), this);
    }
    var taskInfo = my.getStorageSync({ key: 'taskInfo' }).data;
    this.setData({
      minute: query.minute,
      second: query.second,

      houseId: query.houseId,
      taskName: taskInfo.taskName,
      houseNo: query.houseNo,
      selStartTime: taskInfo.selStartTime,

      buyerName: my.getStorageSync({ key: 'nameInputValue' }).data,
      certifyCardType: query.certifyCardType,
      cardId: my.getStorageSync({ key: 'idcardInputValue' }).data,
      totalPrice: query.totalPrice,

      canBuy: canBuy,
      buttonBackground: buttonBackground,

      taskType: my.getStorageSync({ key: 'taskType' }).data
    });
  },
  // 添加收藏
  addCollection() {
    if (this.data.isCollected) {
      web.client.cancelCollection(this.data.houseId, this);
    } else {
      web.client.addCollection(this.data.houseId, this);
    }
  },
  buy() {
    if (this.data.canBuy) {
      this.setData({
        modalOpened: true
      });
    } else {
      var taskId = my.getStorageSync({ key: 'taskId' }).data;
      var buyerStatus = my.getStorageSync({ key: 'buyerStatus' + taskId }).data;
      var toastString = "未轮到您的购买批次，请耐心等待";
      if (buyerStatus == 2) {
        toastString = "您已放弃选" + this.data.taskType + "，不能再次选" + this.data.taskType;
      } else if (buyerStatus == 3) {
        toastString = "您已认购您的" + this.data.taskType + "，不能再次选" + this.data.taskType;
      } else if (buyerStatus == 4) {
        toastString = "此楼盘选" + this.data.taskType + "已结束，不能再次选" + this.data.taskType;
      }
      my.showToast({
        content: toastString,
        duration: 2000
      });
    }
  },
  // 最下面的返回按钮
  back() {
    my.navigateBack({});
  },
  // 提交按钮
  submit() {
    web.client.submitBuy(this.data.houseId, this);
  },
  // 关闭提交对话框
  close() {
    this.setData({
      modalOpened: false
    });
  },
  onUnload() {
    this.setData({
      modalOpened: false
    });
    // 页面被关闭
    clearInterval(interval);
  },
  countTime(minute, second, that) {
    interval = setInterval(function () {
      if (second > 0) {
        second--;
      } else if (minute > 0) {
        minute--;
        second = 59;
      } else {
        console.log("时间到");
        clearInterval(interval);
        // 倒计时结束，不能再购买，按钮置灰
        that.setData({
          canBuy: false,
          buttonBackground: "rgba(38,128,235,1)"
        });
      }
      var lastWarning = false;
      if (minute == 0 && second == 30) {
        lastWarning = true;
      }

      var second1 = second < 10 ? "0" + second : second;
      var minute1 = minute < 10 ? "0" + minute : minute;
      that.setData({
        second: second1,
        minute: minute1,
        lastWarning: lastWarning
      });
    }, 1000);
  },
  closeWarning() {
    this.setData({
      lastWarning: false
    });
  },
  onShareAppMessage() {
    return {
      title: '公证 e 选房',
      desc: '提供在线选房服务',
      path: 'pages/login/login'
    };
  }
});
